]> dgit.raspbian.org Git - gnome-shell.git/commitdiff
environment: Port environment.js to be an ES module
authorEvan Welsh <contact@evanwelsh.com>
Mon, 10 Jul 2023 06:05:14 +0000 (23:05 -0700)
committerEvan Welsh <contact@evanwelsh.com>
Sat, 15 Jul 2023 02:35:56 +0000 (02:35 +0000)
Removes the init() function in favor of executing all environment
changes when the file is imported.

Additionally ports all unit tests using imports.gi.environment.init() to
use the updated module.

Part-of: <https://gitlab.gnome.org/GNOME/gnome-shell/-/merge_requests/2822>

.eslintrc.yml
js/ui/environment.js
js/ui/init.js
tests/unit/highlighter.js
tests/unit/insertSorted.js
tests/unit/jsParse.js
tests/unit/markup.js
tests/unit/signalTracker.js
tests/unit/url.js
tests/unit/versionCompare.js

index 83a8fed311d82d087636797d97a012c36178f88a..16c8ab0947d59891cf7575cdf051a65aa095b3a6 100644 (file)
@@ -4,6 +4,7 @@ extends:
 overrides:
   - files:
       - js/ui/init.js
+      - js/ui/environment.js
       - js/dbusServices/**
       - js/portalHelper/**
       - subprojects/extensions-app/**
index 7e873495e4407ae86ce6a248dba547b094ff951d..0a67b43272515fb17724e14ce5e4ccdc89df1b6e 100644 (file)
@@ -1,22 +1,24 @@
-/* exported init */
-
 // Load all required dependencies with the correct versions
-imports.misc.dependencies; // eslint-disable-line no-unused-expressions
-
-
-const Clutter = imports.gi.Clutter;
-const Gdk = imports.gi.Gdk;
-const Gio = imports.gi.Gio;
-const GLib = imports.gi.GLib;
-const GObject = imports.gi.GObject;
-const Meta = imports.gi.Meta;
-const Polkit = imports.gi.Polkit;
-const Shell = imports.gi.Shell;
-const St = imports.gi.St;
-const Gettext = imports.gettext;
+import '../misc/dependencies.js';
+
+import {setConsoleLogDomain} from 'console';
+import * as Gettext from 'gettext';
+
+import Clutter from 'gi://Clutter';
+import Gdk from 'gi://Gdk';
+import Gio from 'gi://Gio';
+import GLib from 'gi://GLib';
+import GObject from 'gi://GObject';
+import Meta from 'gi://Meta';
+import Polkit from 'gi://Polkit';
+import Shell from 'gi://Shell';
+import St from 'gi://St';
+
 const SignalTracker = imports.misc.signalTracker;
 const {adjustAnimationTime} = imports.misc.animationUtils;
 
+setConsoleLogDomain('GNOME Shell');
+
 Gio._promisify(Gio.DataInputStream.prototype, 'fill_async');
 Gio._promisify(Gio.DataInputStream.prototype, 'read_line_async');
 Gio._promisify(Gio.DBus, 'get');
@@ -28,9 +30,8 @@ Gio._promisify(Gio.File.prototype, 'query_info_async');
 Gio._promisify(Polkit.Permission, 'new');
 
 // We can't import shell JS modules yet, because they may have
-// variable initializations, etc, that depend on init() already having
-// been run.
-
+// variable initializations, etc, that depend on this file's
+// changes
 
 // "monkey patch" in some varargs ClutterContainer methods; we need
 // to do this per-container class since there is no representation
@@ -248,116 +249,114 @@ function _easeActorProperty(actor, propName, target, params) {
     transition.connect('stopped', (t, finished) => callback(finished));
 }
 
-function init() {
-    // Add some bindings to the global JS namespace
-    globalThis.global = Shell.Global.get();
-
-    globalThis._ = Gettext.gettext;
-    globalThis.C_ = Gettext.pgettext;
-    globalThis.ngettext = Gettext.ngettext;
-    globalThis.N_ = s => s;
-
-    GObject.gtypeNameBasedOnJSPath = true;
-
-    GObject.Object.prototype.connectObject = function (...args) {
-        SignalTracker.connectObject(this, ...args);
-    };
-    GObject.Object.prototype.connect_object = function (...args) {
-        SignalTracker.connectObject(this, ...args);
-    };
-    GObject.Object.prototype.disconnectObject = function (...args) {
-        SignalTracker.disconnectObject(this, ...args);
-    };
-    GObject.Object.prototype.disconnect_object = function (...args) {
-        SignalTracker.disconnectObject(this, ...args);
-    };
-
-    SignalTracker.registerDestroyableType(Clutter.Actor);
-
-    // Miscellaneous monkeypatching
-    _patchContainerClass(St.BoxLayout);
-
-    _patchLayoutClass(Clutter.GridLayout, {
-        row_spacing: 'spacing-rows',
-        column_spacing: 'spacing-columns',
-    });
-    _patchLayoutClass(Clutter.BoxLayout, { spacing: 'spacing' });
-
-    let origSetEasingDuration = Clutter.Actor.prototype.set_easing_duration;
-    Clutter.Actor.prototype.set_easing_duration = function (msecs) {
-        origSetEasingDuration.call(this, adjustAnimationTime(msecs));
-    };
-    let origSetEasingDelay = Clutter.Actor.prototype.set_easing_delay;
-    Clutter.Actor.prototype.set_easing_delay = function (msecs) {
-        origSetEasingDelay.call(this, adjustAnimationTime(msecs));
-    };
-
-    Clutter.Actor.prototype.ease = function (props) {
-        _easeActor(this, props);
-    };
-    Clutter.Actor.prototype.ease_property = function (propName, target, params) {
-        _easeActorProperty(this, propName, target, params);
-    };
-    St.Adjustment.prototype.ease = function (target, params) {
-        // we're not an actor of course, but we implement the same
-        // transition API as Clutter.Actor, so this works anyway
-        _easeActorProperty(this, 'value', target, params);
-    };
-
-    Clutter.Actor.prototype[Symbol.iterator] = function* () {
-        for (let c = this.get_first_child(); c; c = c.get_next_sibling())
-            yield c;
-    };
-
-    Clutter.Actor.prototype.toString = function () {
-        return St.describe_actor(this);
-    };
-    // Deprecation warning for former JS classes turned into an actor subclass
-    Object.defineProperty(Clutter.Actor.prototype, 'actor', {
-        get() {
-            let klass = this.constructor.name;
-            let { stack } = new Error();
-            log(`Usage of object.actor is deprecated for ${klass}\n${stack}`);
-            return this;
-        },
-    });
-
-    Gio.File.prototype.touch_async = function (callback) {
-        Shell.util_touch_file_async(this, callback);
-    };
-    Gio.File.prototype.touch_finish = function (result) {
-        return Shell.util_touch_file_finish(this, result);
-    };
-
-    let origToString = Object.prototype.toString;
-    Object.prototype.toString = function () {
-        let base = origToString.call(this);
-        try {
-            if ('actor' in this && this.actor instanceof Clutter.Actor)
-                return base.replace(/\]$/, ` delegate for ${this.actor.toString().substring(1)}`);
-            else
-                return base;
-        } catch (e) {
+// Add some bindings to the global JS namespace
+globalThis.global = Shell.Global.get();
+
+globalThis._ = Gettext.gettext;
+globalThis.C_ = Gettext.pgettext;
+globalThis.ngettext = Gettext.ngettext;
+globalThis.N_ = s => s;
+
+GObject.gtypeNameBasedOnJSPath = true;
+
+GObject.Object.prototype.connectObject = function (...args) {
+    SignalTracker.connectObject(this, ...args);
+};
+GObject.Object.prototype.connect_object = function (...args) {
+    SignalTracker.connectObject(this, ...args);
+};
+GObject.Object.prototype.disconnectObject = function (...args) {
+    SignalTracker.disconnectObject(this, ...args);
+};
+GObject.Object.prototype.disconnect_object = function (...args) {
+    SignalTracker.disconnectObject(this, ...args);
+};
+
+SignalTracker.registerDestroyableType(Clutter.Actor);
+
+// Miscellaneous monkeypatching
+_patchContainerClass(St.BoxLayout);
+
+_patchLayoutClass(Clutter.GridLayout, {
+    row_spacing: 'spacing-rows',
+    column_spacing: 'spacing-columns',
+});
+_patchLayoutClass(Clutter.BoxLayout, {spacing: 'spacing'});
+
+const origSetEasingDuration = Clutter.Actor.prototype.set_easing_duration;
+Clutter.Actor.prototype.set_easing_duration = function (msecs) {
+    origSetEasingDuration.call(this, adjustAnimationTime(msecs));
+};
+const origSetEasingDelay = Clutter.Actor.prototype.set_easing_delay;
+Clutter.Actor.prototype.set_easing_delay = function (msecs) {
+    origSetEasingDelay.call(this, adjustAnimationTime(msecs));
+};
+
+Clutter.Actor.prototype.ease = function (props) {
+    _easeActor(this, props);
+};
+Clutter.Actor.prototype.ease_property = function (propName, target, params) {
+    _easeActorProperty(this, propName, target, params);
+};
+St.Adjustment.prototype.ease = function (target, params) {
+    // we're not an actor of course, but we implement the same
+    // transition API as Clutter.Actor, so this works anyway
+    _easeActorProperty(this, 'value', target, params);
+};
+
+Clutter.Actor.prototype[Symbol.iterator] = function* () {
+    for (let c = this.get_first_child(); c; c = c.get_next_sibling())
+        yield c;
+};
+
+Clutter.Actor.prototype.toString = function () {
+    return St.describe_actor(this);
+};
+// Deprecation warning for former JS classes turned into an actor subclass
+Object.defineProperty(Clutter.Actor.prototype, 'actor', {
+    get() {
+        let klass = this.constructor.name;
+        let {stack} = new Error();
+        log(`Usage of object.actor is deprecated for ${klass}\n${stack}`);
+        return this;
+    },
+});
+
+Gio.File.prototype.touch_async = function (callback) {
+    Shell.util_touch_file_async(this, callback);
+};
+Gio.File.prototype.touch_finish = function (result) {
+    return Shell.util_touch_file_finish(this, result);
+};
+
+const origToString = Object.prototype.toString;
+Object.prototype.toString = function () {
+    let base = origToString.call(this);
+    try {
+        if ('actor' in this && this.actor instanceof Clutter.Actor)
+            return base.replace(/\]$/, ` delegate for ${this.actor.toString().substring(1)}`);
+        else
             return base;
-        }
-    };
-
-    let slowdownEnv = GLib.getenv('GNOME_SHELL_SLOWDOWN_FACTOR');
-    if (slowdownEnv) {
-        let factor = parseFloat(slowdownEnv);
-        if (!isNaN(factor) && factor > 0.0)
-            St.Settings.get().slow_down_factor = factor;
+    } catch (e) {
+        return base;
     }
+};
 
-    // OK, now things are initialized enough that we can import shell JS
-    const Format = imports.format;
+const slowdownEnv = GLib.getenv('GNOME_SHELL_SLOWDOWN_FACTOR');
+if (slowdownEnv) {
+    let factor = parseFloat(slowdownEnv);
+    if (!isNaN(factor) && factor > 0.0)
+        St.Settings.get().slow_down_factor = factor;
+}
 
-    String.prototype.format = Format.format;
+// OK, now things are initialized enough that we can import shell JS
+const Format = imports.format;
 
-    Math.clamp = function (x, lower, upper) {
-        return Math.min(Math.max(x, lower), upper);
-    };
+String.prototype.format = Format.format;
 
-    // Prevent extensions from opening a display connection to ourselves
-    Gdk.set_allowed_backends('');
-}
+Math.clamp = function (x, lower, upper) {
+    return Math.min(Math.max(x, lower), upper);
+};
+
+// Prevent extensions from opening a display connection to ourselves
+Gdk.set_allowed_backends('');
index f71a014d3b85c7240ebb8dcde632656741695020..c605baa6c4eee97ce9e6c89b41695c66d567d482 100644 (file)
@@ -1,10 +1,7 @@
-import {setConsoleLogDomain} from 'console';
 import GLib from 'gi://GLib';
 import Gio from 'gi://Gio';
 
-setConsoleLogDomain('GNOME Shell');
-
-imports.ui.environment.init();
+import './environment.js';
 
 // Run the Mutter main loop after
 // GJS finishes resolving this module.
index 961092c31f0c28d932576786525cd0878b0a815f..ea2daaebf583876e8ab509809d5b6957fc55329e 100644 (file)
@@ -5,9 +5,7 @@
 const JsUnit = imports.jsUnit;
 import Pango from 'gi://Pango';
 
-const Environment = imports.ui.environment;
-Environment.init();
-
+import '../../js/ui/environment.js';
 const Util = imports.misc.util;
 
 const tests = [
index 2fbe6ceb9d86e8dadaeae512a164e338fc0ab52c..5ffc0871f19cd819459ba841936740a642a35dca 100644 (file)
@@ -1,12 +1,13 @@
 /* -*- mode: js2; js2-basic-offset: 4; indent-tabs-mode: nil -*- */
 
 // Test cases for Util.insertSorted
-import * as Assertions from '../common/assertions.js';
 
 // Needed so that Util can bring some UI stuff
 // we don't actually use
-const Environment = imports.ui.environment;
-Environment.init();
+import '../../js/ui/environment.js';
+
+import * as Assertions from '../common/assertions.js';
+
 const Util = imports.misc.util;
 
 let arrayInt = [1, 2, 3, 5, 6];
index cc3d578bc5d9e4690f66095701effb5aef40e924..089cde1a9541a3351b52f7787f983d8209dcc0e0 100644 (file)
@@ -4,9 +4,9 @@
 
 const JsUnit = imports.jsUnit;
 
+import '../../js/ui/environment.js';
+
 import * as Assertions from '../common/assertions.js';
-const Environment = imports.ui.environment;
-Environment.init();
 
 const JsParse = imports.misc.jsParse;
 
index c10f2572d3c5f4f02ba232898d36bc925012787f..9ec737d008729c733e13055d4a4a79bdda666e10 100644 (file)
@@ -5,8 +5,7 @@
 const JsUnit = imports.jsUnit;
 import Pango from 'gi://Pango';
 
-const Environment = imports.ui.environment;
-Environment.init();
+import '../../js/ui/environment.js';
 
 imports.ui.main; // eslint-disable-line no-unused-expressions
 const MessageList = imports.ui.messageList;
index 613fe4e716ae732867b6b1fe147904286c76a22f..4fa157c96193a3614d98bad370377cb3393cc285 100644 (file)
@@ -2,16 +2,14 @@
 
 // Test cases for version comparison
 
+import '../../js/ui/environment.js';
 import GObject from 'gi://GObject';
 
 const JsUnit = imports.jsUnit;
 const Signals = imports.misc.signals;
 
-const Environment = imports.ui.environment;
 const {TransientSignalHolder, registerDestroyableType} = imports.misc.signalTracker;
 
-Environment.init();
-
 const Destroyable = GObject.registerClass({
     Signals: {'destroy': {}},
 }, class Destroyable extends GObject.Object {});
index de9fd0f9d123b79ed8e5cc19234d376677e4fbf5..ac5e1ba32535842e07bb96bde774bc8207eae338 100644 (file)
@@ -4,8 +4,7 @@
 
 const JsUnit = imports.jsUnit;
 
-const Environment = imports.ui.environment;
-Environment.init();
+import '../../js/ui/environment.js';
 
 const Util = imports.misc.util;
 
index b5178181813ef5ab1f40f74cdc6ece96a47dc16d..f0b31d117a37da6f9627c907c3fef7c8d5f61e2a 100644 (file)
@@ -4,8 +4,7 @@
 
 const JsUnit = imports.jsUnit;
 
-const Environment = imports.ui.environment;
-Environment.init();
+import '../../js/ui/environment.js';
 
 const Util = imports.misc.util;